#include "course.h" 
#include <stdio.h>

void ViewCourses(struct Course *courses) 
{
    while(1)
    {
        int numOfCourses = getCourseNum(courses);

        printf("\nThere're totally %d courses recorded:\n\n", numOfCourses);
        for (int i = 0; i < numOfCourses; i++)
        {
            printf("%d.\t%s\t%s\n", i + 1, courses[i].code, courses[i].name);
        }

        printf("\n\nEnter a number to view the related course details or enter any other key back: ");
        int choice;
        scanf("%d", &choice);
        if (choice < 1 || choice > numOfCourses)
        {
            break;
        }

        int index = choice - 1;
        printf("\n\t%s %s\n", courses[index].code, courses[index].name);
        printf("\t--------------------------------------------------------------------------------------------\n");
        printf("\t| No.    | Item                | Percentage | Full Mark  | Grades\n");

        int numOfGradeItems = getGradeItemNum(courses[index]);

        for (int i = 0; i < numOfGradeItems; i++)
        {
            printf("\t| %d      | %-20s| %-11.6f| %-11.6f|", i + 1, courses[index].gradeitems[i].name, courses[index].gradeitems[i].percentage, courses[index].gradeitems[i].fullmark);
            int numOfGrades = getGradeNum(courses[index].gradeitems[i]);

            for (int j = 0; j < numOfGrades; j++)
            {
                printf(" %d: %-7.2f", j + 1, courses[index].gradeitems[i].grades[j]);
            }
            printf("\n");
        }
        printf("\t--------------------------------------------------------------------------------------------\n");

    }
}